package com.xhui.vimarket.zkplus.zul.attr.mod.impl;t java.util.List;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Button;
import org.zkoss.zul.Div;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Menu;
import org.zkoss.zul.Menubar;
import org.zkoss.zul.Menupopup;
import org.zkoss.zul.Span;
import org.zkoss.zul.Textbox;

import com.xhui.crm.common.ResConstants;com.xhui.vimarket.zkplusui.vimarket.zkplus.ui.id.ZKRebcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.com.xhui.vimarket.zkplusil;
import com.xhui.vimarkcom.xhui.vimarket.zkplusttr.AttrExt;
import com.xhui.vimcom.xhui.vimarket.zkplusl.attr.bean.AttrTmpl;
import com.xhui.vimarket.zkplus.zul.attr.mod.Atcom.xhui.vimarket.zkplus/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTextComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrMenuTextComp extends AttrButtonTextComp {
    
    /**
     * 右边按钮，左边文本模式.
     */
    public static final String      STYLE_BTNTEXT    = "buttontext";
    
    /**
     * 右边label，坐标文本和按钮模式.
     */
    public static final String      STYLE_TEXTIMG    = "textimg";
    /**
     * 右边label，坐标文本和pop按钮模式.
     */
    public static final String      STYLE_TEXTPOP    = "textimgpop";
    
    /**
     * 实例.
     */
    private static AttrMenuTextComp inst             = new AttrMenuTextComp();
    
    /**
     * valueId.
     */
    public static final String      TAG_VALUE_ID     = "valueId";
    
    /**
     * TAG_VALUE_OBJECT.
     */
    public static final String      TAG_VALUE_OBJECT = "valueObj";
    
    /**
     * 日志.
     */
    private static Logger           logger           = Logger.getLogger(AttrMenuTextComp.class);
    
    /**
     * 构造函数. 
     */
    private AttrMenuTextComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrMenuTextComp instance() {
        return AttrMenuTextComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @SuppressWarnings("deprecation")
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        // hbox
        final Hbox hbox = new Hbox();
        hbox.setSclass("zh-hbox-widthc");
        hbox.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        hbox.setAttribute(AttrBaseComp.COMP_STYLE, attrTmpl.getDisplayStyle());
        hbox.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        hbox.setAttribute(AttrBaseComp.COMP_JAVACODE, attrTmpl.getJavaCode());
        hbox.setAttribute(AttrBaseComp.COMP_PARAM1, attrTmpl.getParam1());
        hbox.setAttribute(AttrBaseComp.COMP_PARAM2, attrTmpl.getParam2());
        hbox.setAttribute(AttrBaseComp.COMP_CANTNULL, AttrCompUtil.getStrCantNull(attrTmpl
            .isCantNull()));
        hbox.setAttribute(AttrBaseComp.COMP_COMP_NAME, attrTmpl.getName());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_LENGTH, attrTmpl.getValueLength());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_FROM, attrTmpl.getValueFrom());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_TO, attrTmpl.getValueTo());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_REX, attrTmpl.getValidExpr());
        hbox.setWidth("100%");
        hbox.setWidths(this.getWidthsLCT(attrTmpl));
        final Div div = new Div();
        // add to zul
        hbox.setParent(comp);
        div.setParent(hbox);
        Span span = new Span();
        span.setWidth("1px");
        span.setParent(div);
        // Executions.createComponentsDirectly("<span width=\"1px\" />", null, div, null); // popup parent
        //        final String sButtonWithComposer = attrExt.getAttrAda().getAttrTmplCompStr(
        //            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null);
        //        final Button button = (Button) Executions.createComponentsDirectly(sButtonWithComposer,
        //            null, div, null);
        final Button button = (Button) attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, div);
        //button.setParent(div);
        button.setLabel(attrTmpl.getName());
        ZKRebuildUuid.rebuildUuidById(button, "button_"
            + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId()
                : "_" + Math.abs(attrTmpl.getAttrId())));
        Span span2 = new Span();
        span2.setWidth("5px");
        span2.setParent(div);
        // Executions.createComponentsDirectly("<span width=\"5px\" />", null, div, null);
        final Label label = new Label(attrTmpl.getName());
        label.setParent(div);
        label.setSclass("zp-lt-label");
        if (attrTmpl.getHint() != null) {
            hbox.setTooltiptext(attrTmpl.getHint());
        }
        // menubar
        //        final String smButtonWithComposer = attrExt.getAttrAda().getAttrMenuCompStr(
        //            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null);
        //        final Menubar menubar = (Menubar) Executions.createComponentsDirectly(smButtonWithComposer,
        //            null, div, null);
        final Menubar menubar = (Menubar) attrExt.getAttrAda().getAttrMenuCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, div);
        final Menu menu = (Menu) menubar.getFirstChild();
        menu.setLabel(attrTmpl.getName());
        //menubar.setSclass("zp-lt-label");
        menubar.setSclass("zm-menu-right");
        
        ZKRebuildUuid.rebuildUuidById(menubar, "menubar_"
            + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId()
                : "_" + Math.abs(attrTmpl.getAttrId())));
        // menu.setSclass("zp-lt-label");
        // info
        final Span infDiv = this.createRulebar(attrExt, div, attrTmpl);
        
        /*final Button refreshbutton = (Button) Executions.createComponentsDirectly(
            "<button apply=\"" + AttrButtonTextComp.REFESH_BTN_COMPOSER + "\" />", null, div, null);
        refreshbutton.setLabel("x");*/
        final Div divt = new Div();
        divt.setParent(hbox);
        
        final Textbox textbox = new Textbox();
        ComposerUtil.bindComposer(textbox, AttrButtonTextComp.TXT_COMPOSER, divt);
        //        final Textbox textbox = (Textbox) Executions.createComponentsDirectly("<textbox apply=\""
        //            + AttrButtonTextComp.TXT_COMPOSER + "\" />", null, divt, null);
        textbox.setWidth(AttrBaseComp.WIDTH_BTNTEXT);
        //if (attrTmpl.isReadOnly()) {
        textbox.setReadonly(true);
        //}
        if (AttrButtonTextComp.BTN_USIM_COMPOSER.equals(attrTmpl.getParam2())) {
            textbox.setReadonly(false);
        }
        //        final Button btnimg = (Button) Executions.createComponentsDirectly(sButtonWithComposer,
        //            null, divt, null);
        final Button btnimg = (Button) attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, divt);
        //button.setParent(divt);
        // btnimg.setLabel("@");
        btnimg.setImage(ResConstants.IMG_FIND);
        btnimg.setStyle("width:28px;height:24px;");
        btnimg.setAttribute(AttrButtonTextComp.STYLE_TEXTIMG, 1);
        Span span3 = new Span();
        span3.setWidth("5px");
        span3.setParent(divt);
        // Executions.createComponentsDirectly("<span width=\"5px\" />", null, divt, null);
        Button button2 = new Button();
        ComposerUtil.bindComposer(button2, AttrButtonTextComp.REFESH_BTN_COMPOSER, divt);
        button2.setLabel("X");
        //        Executions.createComponentsDirectly("<button apply=\""
        //            + AttrButtonTextComp.REFESH_BTN_COMPOSER + "\" label=\"X\" />", null, divt, null);
        
        // comp log
        if (AttrMenuTextComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<hbox width=\"100%\" widths=\"40%,60%\" >");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_JAVACODE, "" + attrTmpl.getJavaCode());
            attrExt.appendInnerComp("<button label=\"");
            attrExt.appendInnerComp(attrTmpl.getName());
            attrExt.appendInnerComp("\" />");
            attrExt.appendInnerComp("<textbox ");
            if (attrTmpl.isReadOnly()) {
                attrExt.appendInnerComp("readonly=\"true\"");
            }
            attrExt.appendInnerComp("/>");
            attrExt.appendInnerComp("</hbox>");
        }
        // toolbox
        this.createToolbox(hbox, attrExt.isToolboxVisible(), attrTmpl);
        
        // 模式选择
        if (attrExt.getAttrAda().isViewMode()) { // 展示模式
            label.setVisible(true);
            btnimg.setVisible(false);
            button.setVisible(false);
            menubar.setVisible(false);
        } else if (AttrButtonTextComp.STYLE_TEXTIMG.equals(attrTmpl.getDisplayStyle() + "")
            || AttrButtonTextComp.STYLE_TEXTPOP.equals(attrTmpl.getDisplayStyle() + "")) {
            label.setVisible(false);
            button.setVisible(false);
        } else {
            button.setVisible(false);
            label.setVisible(false);
            btnimg.setVisible(false);
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    /**
     * getMenubar.
     * 
     * @param root Component
     * @return Textbox
     */
    public static Menubar getMenubar(Component root) {
        // Menubar
        // hbox // div(0) // button(4)
        List<Component> compList = root.getFirstChild().getChildren();
        if (compList != null && compList.size() > 4) {
            if ((root.getFirstChild().getChildren().get(4)) instanceof Menubar) {
                return (Menubar) (root.getFirstChild().getChildren().get(4));
            }
        }
        return null;
    }
    
    /**
     * getMenubar.
     * 
     * @param root Component
     * @return Textbox
     */
    public static Menupopup getMenupopup(Component root) {
        // Menupopup
        // hbox // div(0) // button(0)
        Menubar menubar = getMenubar(root);
        if (menubar != null) {
            return (Menupopup) menubar.getFirstChild().getFirstChild();
        }
        return null;
    }
    
}
